package com.simulation.web.controller.tool;

import com.simulation.common.core.controller.BaseController;
import com.simulation.common.core.domain.AjaxResult;
import com.simulation.common.core.domain.entity.SysUser;
import com.simulation.common.core.domain.model.LoginUser;
import com.simulation.common.dto.CodeWorkspaceDTO;
import com.simulation.common.utils.CoderUtils;
import com.simulation.common.utils.GiteaUtils;
import com.simulation.common.utils.StringUtils;
import com.simulation.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;

/**
 * @author zhangxn
 * @date 2024/12/27  21:16
 */
@RestController
@RequestMapping("/gitea/tool")
public class GiteaController  extends BaseController {

    @Resource
    private ISysUserService sysUserService;

    @ApiOperation("创建仓库")
    @GetMapping("/createRepo")
    @PermitAll
    public AjaxResult createRepo(@RequestParam("repoName")String repoName) {
        // 我启动coder
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = sysUserService.selectUserPassnoById(loginUser.getUserId());
        String result = GiteaUtils.createGiteaRepo(sysUser.getUserName(), sysUser.getPassno(),repoName);
        return AjaxResult.success("操作成功", result);
    }


    @ApiOperation("创建分支")
    @GetMapping("/createRepoBranch")
    @PermitAll
    public AjaxResult createRepoBranch(@RequestParam("repoName")String repoName,@RequestParam("branchName")String branchName) {
        // 我启动coder
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = sysUserService.selectUserPassnoById(loginUser.getUserId());
        String result = GiteaUtils.createGiteaRepoBranch(sysUser.getUserName(), sysUser.getPassno(),repoName,branchName);
        return AjaxResult.success("操作成功", result);
    }

    @ApiOperation("删除分支")
    @GetMapping("/delRepoBranch")
    @PermitAll
    public AjaxResult deleteRepoBranch(@RequestParam("repoName")String repoName,@RequestParam("branchName")String branchName) {
        // 我启动coder
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = sysUserService.selectUserPassnoById(loginUser.getUserId());
        String result = GiteaUtils.delGiteaRepoBranch(sysUser.getUserName(), sysUser.getPassno(),repoName,branchName);
        return AjaxResult.success("操作成功", result);
    }


    @ApiOperation("fork分支")
    @GetMapping("/forkRepo")
    @PermitAll
    public AjaxResult forkRepo(@RequestParam("repoName")String repoName,@RequestParam("ownerName")String ownerName) {
        // 我启动coder
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = sysUserService.selectUserPassnoById(loginUser.getUserId());
        String result = GiteaUtils.getForkRepoGiteaRepo(sysUser.getUserName(), sysUser.getPassno(),repoName,ownerName);
        return AjaxResult.success("操作成功", result);
    }
}
